import { ArrowDownWideNarrow, CheckCheck, RefreshCcwDot, StepForward, WrapText } from "lucide-react";
import { useEditor } from "novel";
import { getPrevText } from "novel/utils";
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator,
} from "@/components/ui/command"

const options = [
  {
    value: "improve",
    label: "文稿润色",
    icon: RefreshCcwDot,
  },
  {
    value: "fix", 
    label: "语法检查",
    icon: CheckCheck,
  },
  {
    value: "shorter",
    label: "文稿缩写", 
    icon: ArrowDownWideNarrow,
  },
  {
    value: "longer",
    label: "文稿扩写",
    icon: WrapText,
  },
];

interface AISelectorCommandsProps {
  onSelect: (value: string, option: string) => void;
}

const AISelectorCommands = ({ onSelect }: AISelectorCommandsProps) => {
  const { editor } = useEditor();

  return (
    <Command>
      <CommandList>
        <CommandGroup heading="Edit or review selection">
          {options.map((option) => (
            <CommandItem
              key={option.value}
              onSelect={(value) => {
                if (!editor) return;
                const slice = editor.state.selection.content();
                const text = editor.storage.markdown.serializer.serialize(slice.content);
                onSelect(text, value);
              }}
              value={option.value}
            >
              <option.icon className="mr-2 h-4 w-4 text-purple-500" />
              {option.label}
            </CommandItem>
          ))}
        </CommandGroup>
        <CommandSeparator />
        <CommandGroup heading="Use AI to do more">
          <CommandItem
            onSelect={() => {
              console.log('Generate button clicked');
              if (!editor) {
                console.error('Editor instance is null');
                return;
              }
              const pos = editor.state.selection.from;
              console.log('Cursor position:', pos);
              
              const text = getPrevText(editor, pos);
              console.log('Text to generate:', text);
              onSelect(text, "generate");
            }}
            value="generate"
          >
            <StepForward className="mr-2 h-4 w-4 text-purple-500" />
            生成文案(todo)
          </CommandItem>
        </CommandGroup>
      </CommandList>
    </Command>
  );
};

export default AISelectorCommands;
